<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
      <el-form label-position="right" :model="contractForm" :label-width="formLabelWidth" disabled="disabled">
        <el-row>
          <el-col :span="11">
            <el-form-item label="员工姓名">
              <el-input v-model="contractForm.employeeName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="合同签订日期">
              <el-input v-model="contractForm.contractSignDate"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="合同类型">
              <el-select v-model="contractForm.contractType" placeholder="请选择合同类型">
                <el-option label="全职" :value="10"></el-option>
                <el-option label="兼职" :value="20"></el-option>
                <el-option label="实习" :value="30"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="生效日期">
              <el-input v-model="contractForm.effectiveDate"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="到期日期">
              <el-input v-model="contractForm.expiryDate"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="合同状态">
              <el-select v-model="contractForm.state" placeholder="请选择合同状态">
                <el-option label="有效" :value="10"></el-option>
                <el-option label="终止" :value="20"></el-option>
                <el-option label="续签" :value="30"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="工作地点">
              <el-input v-model="contractForm.workLocation"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="工作时间">
              <el-input v-model="contractForm.workingHours"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="岗位职责" prop="jobDescription">
              <el-input type="textarea" :rows="3" v-model="contractForm.jobDescription"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="创建时间">
              <el-input v-model="contractForm.createTime"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="最后修改时间">
              <el-input v-model="contractForm.modifyTime"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="12" :offset="6">
          <div class="form-button-container">
            <el-button @click="onBack" type="primary">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup lang="ts">
import {defineEmits, defineProps, onMounted, reactive, ref, inject, toRefs} from 'vue';
import contractApi from '@/api/contractApi'
import type {Contract} from "@/types/resp/contract";
import type {Id} from "@/types/id"

const contractForm = reactive<Partial<Contract>>({});
const props = defineProps<Id>()
const emit = defineEmits<{
  (e: 'closeContractViewEvent'): void;
}>()
const formLabelWidth = ref<string>('')

onMounted(() => {
  initContractById(props.id)
  const globalState = inject('globalState', {} as { formLabelWidth?: string })
  if (!globalState) {
    throw new Error('globalState is not provided');
  }
  const globalStateRefs = toRefs(globalState)
  if (globalStateRefs.formLabelWidth) {
    formLabelWidth.value = globalStateRefs.formLabelWidth.value || ''
  }
})

const initContractById = async (id: number) => {
  const resp = await contractApi.findById(id)
  if (resp && resp.code === 1 && resp.data) {
    Object.assign(contractForm, resp.data)
  }
}
const onBack = () => {
  emit('closeContractViewEvent')
}
</script>

<style lang="scss">
</style>